#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

 dwrd lof;
 word x,y;
 byte chr;
 byte a,b,xb;

 byte d;
 byte tmp[65535];

 FILE *f1,*f2;

 char fil1[80],*ep;
// char fil1[80]="c:/romhack/_8x8_.lzb",*ep;
 char fil2[80];

byte Instr(char *s, byte, char);
void Convert(char *,byte,byte);
void Lower(char *s);

void main(argc,argv)
int argc;
char *argv[];
{
  if (argc > 1){
	strcpy(fil1,argv[1]);
  }// END IF

  clrscr();
  Convert(fil1,'\\','/');

  f1 = fopen( fil1, "rb" );		// Binary file
  assert( f1 );

  fseek( f1, 0, SEEK_END );
	lof = ftell(f1);
	fseek( f1, 0, SEEK_SET );
//  fread(tmp,1,lof,f1);
//	close( f1 );

  strcpy( fil2, fil1 );
  strcpy( &fil2[ Instr( fil2, '.', 1) + 1 ], "c" );

  f2 = fopen( fil2,"wt" );		// text file
  assert( f2 );
  Lower( fil1 );

	fprintf(f2," unsigned char FONT[0x%lX] = {\n    ", lof );
  //
  x = 0;
  y = 0;
  while( lof-- > 0 )
  { //
    d = fgetc( f1 );
    if ( x < 15 ){
      fprintf( f2, "0x%02X, ", d );
      x++;
    } else {
      fprintf( f2, "0x%02X, // %04X\n    ", d, y++ );
      x = 0;
    }
/*  	while( ++y < 16 )
	  {
	    fread( d, 1, 16, f1 );
	    if ( y < 15)
	      fprintf( f2,
       	         " %02d,%02d,%02d,%02d,%02d,%02d,%02d,%02d,"
		             "%02d,%02d,%02d,%02d,%02d,%02d,%02d,%02d,\n",
		             d[0],d[1],d[2],d[3],d[4],d[5],d[6],d[7],
		             d[8],d[9],d[10],d[11],d[12],d[13],d[14],d[15]
							 );
	    else
	      fprintf( f2,
		             " %02d,%02d,%02d,%02d,%02d,%02d,%02d,%02d,"
		             "%02d,%02d,%02d,%02d,%02d,%02d,%02d,%02d,\n \n\n",
          		   d[0],d[1],d[2],d[3],d[4],d[5],d[6],d[7],
	  	           d[8],d[9],d[10],d[11],d[12],d[13],d[14],d[15]
				       );
  	}// WEND

    //
    lof -= 16;
*/
  }// WEND

	fprintf(f2,"\n  }\n" );

  fcloseall();
}// END OF MAIN



/******************************
; Finds char in string        *
******************************/
byte Instr(char *s, byte chr, char way)
{

 if (way == 0){
	xb = 0;
	while(s[xb] != chr && xb < strlen(s)){
	 xb++;
	}// WEND
 }else{
	xb = strlen(s);
	while(s[xb] != chr && xb > 0){
	 xb--;
	}// WEND
 }// END IF
 return xb;
}


void Convert(char *s,byte out,byte in)
{
 byte xx;
 xx=-1;

 while(++xx < strlen(s))
   s[xx] = ((s[xx] == out) ? in : s[xx]);
}// END OF SUB



void Lower(char *s)
{
 byte xx;
 xx=-1;

 while(++xx < strlen(s))
   s[xx] = tolower(s[xx]);
}// END OF SUB